using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FeedAntGame.Control;
using FeedAntGame.Sprite.Event;
using FeedAntGame.Sprite;
using System.Collections;
using FeedAntGame.Support;


namespace FeedAntGame.Component.Dialog
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class HighScoreDlg : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D bg;
        SpriteBatch spriteBatch;
        SpriteFont font;
        Button okButton;
        Vector2 bgPos;
        Vector2[] rowPos = new Vector2[3];
        int numRow = 5;
        int rowDistance = 20;
        public HighScoreDlg(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
          
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
 

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            font = Game.Content.Load<SpriteFont>(@"Fonts\gridValue");
            //bg
            bg = Game.Content.Load<Texture2D>(@"Images\high_scores_background");
            bgPos = new Vector2(Game.Window.ClientBounds.Width / 2 - bg.Width / 2,
                Game.Window.ClientBounds.Height / 2 - bg.Height / 2 - 25);
            
            //ok button
            okButton = new Button(Game, Button.TYPE.OK, bgPos + new Vector2(125, 230));
            EventSprite okSprite = (EventSprite)okButton.Sprite;
            okSprite.Scale = 0.5f;
            okSprite.Click += new EventSprite.ClickButton(onOkClick);
            okSprite.Hover += new EventSprite.HoverButton(onShowHand);
            //Row position

            rowPos[0] = bgPos + new Vector2(30, 95);
            rowPos[1] = bgPos + new Vector2(160, 95);
            rowPos[2] = bgPos + new Vector2(250, 95);  
            base.LoadContent();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            okButton.Sprite.Update(gameTime, Game.Window.ClientBounds);
            if ((okButton.Sprite as EventSprite).IsPlaySoundHover())
            {
                ((GameFA)Game).PlayCue(okButton.Sprite.collisionCueName);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(bg, new Rectangle((int)bgPos.X, (int)bgPos.Y, bg.Width, bg.Height), Color.White);
            for (int i = 0; i < numRow; i++)
            {
                showHighScores(i);
            }
            okButton.Sprite.Draw(gameTime, spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void onOkClick()
        {
            Finish();
        }
        private void onShowHand()
        {
            ((GameFA)Game).Cursor.State = Cursor.STATE_TYPE.HAND;
        }

        public void Start()
        {
            this.Enabled = true;
            this.Visible = true;
            ((GameFA)Game).Volume(0, "Music");
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (!(c is HighScoreDlg) && c.Visible == true && c.Enabled == true)
                {
                    c.Enabled = false;
                }
            }
        }

        private void Finish()
        {
            this.Enabled = false;
            this.Visible = false;
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (!(c is HighScoreDlg) && c.Visible == true && c.Enabled == false)
                {
                    c.Enabled = true;
                }
            }
            ((GameFA)Game).Volume(1, "Music");
        }

        private void showHighScores(int iRow)
        {
            List<ArrayList> hs = Database.HighScore.GetAllRank();
            ArrayList info = null;
            foreach (ArrayList row in hs)
            {
                int rank = Int32.Parse(row[0].ToString());
                if (rank == iRow + 1)
                {
                    info = row;
                    break;
                }
                
            }
            string player = info[1].ToString();
            string level = info[2].ToString();
            string score = info[3].ToString();
            spriteBatch.DrawString(font, player, rowPos[0] + new Vector2(0, iRow * rowDistance), Color.White);
            spriteBatch.DrawString(font, level, rowPos[1] + new Vector2(0, iRow * rowDistance), Color.White);
            spriteBatch.DrawString(font, score, rowPos[2] + new Vector2(0, iRow * rowDistance), Color.White);
        }
    }
}
